
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_exti.h"
#include "misc.h"
static unsigned short step = 50;
static signed char direction = 1;


void begin()
{
    TIM_TimeBaseInitTypeDef TimerInitStruct;
    NVIC_InitTypeDef        NVICInitStruct;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOE , ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
    TimerInitStruct.TIM_ClockDivision = 0;
    TimerInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TimerInitStruct.TIM_Period = 2000;
    TimerInitStruct.TIM_Prescaler = 72 - 1;
    TIM_TimeBaseInit(TIM4, &TimerInitStruct);
    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM4, ENABLE);
    NVICInitStruct.NVIC_IRQChannel = TIM4_IRQn;
    NVICInitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVICInitStruct.NVIC_IRQChannelPreemptionPriority = 1;
    NVICInitStruct.NVIC_IRQChannelSubPriority = 2;
    NVIC_Init(&NVICInitStruct);
}

void GPIO_init()
{
    NVIC_InitTypeDef        NVICInitStruct;
    GPIO_InitTypeDef GPIOtypeInitStruct;
    GPIOtypeInitStruct.GPIO_Mode = GPIO_Mode_IPU;
    GPIOtypeInitStruct.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15;
    GPIOtypeInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIOtypeInitStruct);
    GPIOtypeInitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIOtypeInitStruct.GPIO_Pin = 0xff;
    GPIOtypeInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOE, &GPIOtypeInitStruct); 
    NVICInitStruct.NVIC_IRQChannel = EXTI15_10_IRQn;
    NVICInitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVICInitStruct.NVIC_IRQChannelPreemptionPriority = 1;
    NVICInitStruct.NVIC_IRQChannelSubPriority = 2;
    NVIC_Init(&NVICInitStruct);    
      EXTI_InitTypeDef EXTI_InitStructure;
       EXTI_ClearITPendingBit(EXTI_Line14);
       EXTI_ClearITPendingBit(EXTI_Line15);

       //选择中断管脚PC.2 PC.3 PC.5
       GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);
       GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource15);

       EXTI_InitStructure.EXTI_Line = EXTI_Line14 | EXTI_Line15; //选择中断线路2 3 5
       EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断请求，非事件请求
       EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //设置中断触发方式为上下降沿触发
       EXTI_InitStructure.EXTI_LineCmd = ENABLE;                                          //外部中断使能
      EXTI_Init(&EXTI_InitStructure);
}

int main()
{
  int s= 0;
  char lastkey1 = 0;
  char lastkey2 = 0;
  char key1 = 0;
  char key2 = 0;
  begin();
   GPIO_init();
    while(1)
    {
        // key1 = ((GPIOB->IDR & (1 << 14)) >> 14);
        // key2 = ((GPIOB->IDR & (1 << 15)) >> 15);
        // if(!key1 && lastkey1 != key1)
        // {
        //     step += 5;
        // }
        // if(!key2 && lastkey2 != key2)
        // {
        //     if(step > 5)
        //         {step -= 5;}
        // }
        // lastkey1 = key1;
        // lastkey2 = key2;
        // for(s = 0; s < 20000;s ++);
    }
}


static unsigned short count = 0;

void next()
{
    static char ledindex = 0;
    unsigned char tmp;
    ledindex += direction;
    ledindex &= 7;
    GPIOE->ODR = 0;
    GPIOE->ODR |= (1 << ledindex);
}

void TIM4_IRQHandler()
{
    TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
    count ++;
    if(count >= step)
    {
        count = 0;
        next();
    }

}
void EXTI15_10_IRQHandler()
{
    short s = 0;
    if((GPIOB->IDR & (1 << 14)) == 0)
    {
        for(s = 0; s < 20000;s ++);
        EXTI_ClearITPendingBit(EXTI_Line14);
    }
    else if((GPIOB->IDR & (1 << 15)) == 0)
    {
        for(s = 0; s < 20000;s ++);
        EXTI_ClearITPendingBit(EXTI_Line15);
    }

}
